package com.example.demo02.utils;



import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by batwings on 16/01/02.
 */
public class DateUtils {

    //日期格式设定
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat dateFormatShort = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat dateFormatShort2 = new SimpleDateFormat("yyMMddHHmmss");
    public static SimpleDateFormat dateFormatShort3 = new SimpleDateFormat("yy");
    public static SimpleDateFormat dateFormatShort4 = new SimpleDateFormat("yyMMdd");
    public static SimpleDateFormat dateFormatShort6 = new SimpleDateFormat("HHmmss");
    public static SimpleDateFormat dateFormatShorthh = new SimpleDateFormat("HH");
    public static SimpleDateFormat dateFormatShortmm = new SimpleDateFormat("mm");
    public static SimpleDateFormat dateFormatShortSS = new SimpleDateFormat("yyyyMMddHHmmssS");

    //日期带时间格式设定
    public static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat dateTimeFormat0 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat dateTimeFormats = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
    public static SimpleDateFormat dateTimeFormats2 = new SimpleDateFormat("M月d日HH:mm");
    public static SimpleDateFormat dateTimeFormat1 = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
    public static SimpleDateFormat dateFormatShort5 = new SimpleDateFormat("yyMM");
    public static SimpleDateFormat dateFormatShort7 = new SimpleDateFormat("MM-dd");
    public static SimpleDateFormat dateFormatShort8 = new SimpleDateFormat("yyyy/m/d h:mm");

    //统计报表时间模式
    public static String STATISTIC_TIME_MODE_PERIOD = "2";
    public static String STATISTIC_TIME_MODE_DAY = "1";
    public static String STATISTIC_TIME_MODE_MONTH = "3";
    public static String STATISTIC_TIME_MODE_YEAR = "4";
    public static String STATISTIC_TIME_MODE_TOTAL = "0";

    public static Date parseStrYYYYMMdd(String datestr) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(getDateFormat(11)); //规定日期格式
            Date date = formatter.parse(datestr); //将符合格式的String转换为Date
            return date;
        } catch (Exception e) {
        }
        return null;
    }

    public static Date parseDateStr(String datestr, int type) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(getDateFormat(type)); //规定日期格式
            Date date = formatter.parse(datestr); //将符合格式的String转换为Date
            return date;
        } catch (Exception e) {
        }
        return null;
    }

    public static Date parseStrYYYYMMDDHHMMSS(String datestr) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(getDateFormat(12)); //规定日期格式
            Date date = formatter.parse(datestr); //将符合格式的String转换为Date
            return date;
        } catch (Exception e) {
        }
        return null;
    }

    public static String add0am(String date) {
        if (date != null) {
            return date + " 00:00:00";
        } else {
            return null;
        }
    }

    public static String add23pm(String date) {
        if (date != null) {
            return date + " 23:59:59";
        } else {
            return null;
        }
    }

    public static String formatDateYYYYMMDDHHMMSS(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        } else {
            return "";
        }

    }

    public static String formatDateYYYYMMDDHHMM(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return sdf.format(date);
        } else {
            return "";
        }
    }

    public static String formatDateYYYYMMDD(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String formatHHMMSS(Date date){
        if(date == null){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }

    public static String formatDateYYYYMM(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return sdf.format(date);
    }

    public static String formatDateYYYYMMdd(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    public static String formatDateHH(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("H");
        return sdf.format(date);
    }

    public static String formatDatemm(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("m");
        return sdf.format(date);
    }

    public static String formatDateYYYY(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(date);
    }

    public static String formatDatedd(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        return sdf.format(date);
    }

    public static String formatDateMM(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        return sdf.format(date);
    }


    /**
     * 给指定日期加分钟
     *
     * @param date
     * @param x
     * @return
     */
    public static Date addDateMinut(Date date, int x)//返回的是字符串型的时间，输入的
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
        //引号里面个格式也可以是 HH:mm:ss或者HH:mm等等，很随意的，不过在主函数调用时，要和输入的变
        //量day格式一致
        //System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, x);// 24小时制
        date = cal.getTime();
        // System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return date;

    }



    public static Date formatstr2(String datestr) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //规定日期格式
            Date date = formatter.parse(datestr); //将符合格式的String转换为Date
            return date;
        } catch (Exception e) {
        }
        return new Date();
    }

    public static Date formatstr3(String datestr) {
        Date date = new Date();
        try {

            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); //规定日期格式
            date = formatter.parse(datestr); //将符合格式的String转换为Date

        } catch (Exception e) {
        }

        return date;
    }

    public static Date formatstr(String datestr) {
        Date date = new Date();
        try {

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //规定日期格式
            date = formatter.parse(datestr); //将符合格式的String转换为Date

        } catch (Exception e) {
        }

        return date;
    }

    public static String getDate(Date date, int kind) {
        String res = "";
        if (date != null) {
            String format = getDateFormat(kind);
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            res = sdf.format(date);
        }
        return res;
    }

    private static String getDateFormat(int kind) {
        String[] format = {
                "yyyy-MM-dd", //0
                "yyyy-MM-dd HH:mm:ss", //1
                "yyyy",//2
                "M",//3
                "dd", //4
                "yyyy年M月d日H点m分", //5
                "yyyy年M月d日", //6
                "H点m分", //7
                "yyyy-MM-dd HH:mm", //8
                "HH",//9
                "mm",//10
                "yyyyMMdd", //11
                "yyyyMMddHHmmss", //12
                "yyyy-MM-dd 23:59:59", //13
                "yyyy 年 M 月 d 日", //14
                "yyyy年MM月dd日HH时mm分",//15
                "yyyy 年  M 月  d 日  H 点  m 分", //16
                "HHmmss",//17
                "HHmm",//18
                "HH:mm",//19
                "yyyyMM",//20
                "HH:mm:ss",//21
                "yyyy-MM-dd HH:mm" //22
        };
        return format[kind];
    }

    /**
     * 格式化当前时间为 dateStyle 样式
     *
     * @param dateStyle
     * @return
     */
    public static String formatDateByStyle(String dateStyle) {
        String str = "";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(dateStyle); //规定日期格式
            str = formatter.format(new Date()); //
        } catch (Exception e) {
            return null;
        }
        return str;
    }

    /**
     * 格式化传入时间为 dateStyle 样式
     *
     * @param date
     * @param dateStyle
     * @return
     */
    public static String formatDateByStyle(Date date, String dateStyle) {
        String str = "";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(dateStyle); //规定日期格式
            str = formatter.format(date); //
        } catch (Exception e) {
            return null;
        }
        return str;
    }

    /**
     * 字符串转换为日期java.util.Date
     *
     * @param dateText 字符串
     * @param kind     日期格式
     * @return
     */
    public static Date toDate(String dateText, int kind) {
        String format = getDateFormat(kind);
        if (dateText == null) {
            return null;
        }
        DateFormat df = null;
        try {
            if (format == null) {
                df = new SimpleDateFormat();
            } else {
                df = new SimpleDateFormat(format);
            }

            // setLenient false to avoids allowing dates like 9/32/2001
            // and true to allowing, which would otherwise parse to 10/2/2001
            df.setLenient(false);

            return df.parse(dateText);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween2(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static String formatDateYYYYMMdd(String date) {
        if (date != null && !"".equals(date)) {
            return formatDateYYYYMMdd(formatstr2(date));
        } else {
            return "";
        }
    }

    public static String formatTransdate(String dateStr) {
        if (dateStr != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
                Date date = sdf.parse(dateStr); //将符合格式的String转换为Date
                return formatDateYYYYMMDDHHMMSS(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static String formatdate(String dateStr) {
        if (dateStr != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                Date date = sdf.parse(dateStr); //将符合格式的String转换为Date
                return formatDateYYYYMMDD(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static String formatdateYYYYMMDDHHMMSS(String dateStr) {
        if (dateStr != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = sdf.parse(dateStr); //将符合格式的String转换为Date
                return formatDateYYYYMMDDHHMMSS(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /**
     * 得到当前的日期
     */
    public static int getDay() {
        return Calendar.getInstance().get(Calendar.DATE);
    }

    //获取当前日期
    public static String getNowDateShortSS() {
        String ret = dateFormatShortSS.format(new Date());
        if (ret == null) {
            ret = "";
        }
        return ret;
    }

    public static void main(String[] a) {
        System.out.println(formatstr2("2022-12-15").after(new Date()));
    }

    public static Date parseStrYYYYMMDDHHMM(String datestr) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(getDateFormat(8)); //规定日期格式
            Date date = formatter.parse(datestr); //将符合格式的String转换为Date
            return date;
        } catch (Exception e) {
        }
        return null;
    }

    //获取月份的天数
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    //获取两个字符串日期相差的天数
    public static List<String> getBetweenDates(String start, String end) {
        List<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date start_date = sdf.parse(start);
            Date end_date = sdf.parse(end);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start_date);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end_date);
            while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    //获取两个字符串日期相差的天数
    public static List<String> getBetweenYears(String start, String end) {
        List<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            Date start_date = sdf.parse(start);
            Date end_date = sdf.parse(end);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start_date);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end_date);
            while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    //获取两个字符串日期相差的天数
    public static List<String> getBetweenMonths(String start, String end) {
        List<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date start_date = sdf.parse(start);
            Date end_date = sdf.parse(end);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start_date);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end_date);
            while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.MONTH, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String formatDateHHmmss(String date) {
        if (date != null && !"".equals(date)) {
            return formatDateHHMMss(formatstr4(date));
        } else {
            return "";
        }
    }


    public static String formatDateHHMMss(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
        return sdf.format(date);
    }

    public static Date formatstr4(String datestr) {
        Date date = new Date();
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //规定日期格式
            date = formatter.parse(datestr); //将符合格式的String转换为Date

        } catch (Exception e) {
        }

        return date;
    }

    /**
     * 给指定日期加天数
     *
     * @param date
     * @param x
     * @return
     */
    public static Date addDateDay(Date date, int x)//返回的是字符串型的时间，输入的
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
        //引号里面个格式也可以是 HH:mm:ss或者HH:mm等等，很随意的，不过在主函数调用时，要和输入的变
        //量day格式一致
        //System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, x);// 24小时制
        date = cal.getTime();
        // System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return date;

    }

    public static String getLastMonth(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - num); // 设置为上一个月
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    public static String getDateFormatTypeByStatictisMode(String mode) {
        if (DateUtils.STATISTIC_TIME_MODE_YEAR.equals(mode)) {
            return "%Y";
        } else if (DateUtils.STATISTIC_TIME_MODE_MONTH.equals(mode)) {
            return "%Y-%m";
        }
        return "%Y-%m-%d";
    }

}
